package org.dromara.commission.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.commission.domain.bo.LzshCommissionHeaderBo;
import org.dromara.commission.domain.vo.LzshCommissionHeaderVo;
import org.dromara.commission.domain.LzshCommissionHeader;
import org.dromara.commission.mapper.LzshCommissionHeaderMapper;
import org.dromara.commission.service.ILzshCommissionHeaderService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 佣金清单字段Service业务层处理
 *
 * @author Lion Li
 * @date 2024-08-12
 */
@RequiredArgsConstructor
@Service
public class LzshCommissionHeaderServiceImpl implements ILzshCommissionHeaderService {

    private final LzshCommissionHeaderMapper baseMapper;

    /**
     * 查询佣金清单字段
     *
     * @param id 主键
     * @return 佣金清单字段
     */
    @Override
    public LzshCommissionHeaderVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询佣金清单字段列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 佣金清单字段分页列表
     */
    @Override
    public TableDataInfo<LzshCommissionHeaderVo> queryPageList(LzshCommissionHeaderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LzshCommissionHeader> lqw = buildQueryWrapper(bo);
        Page<LzshCommissionHeaderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的佣金清单字段列表
     *
     * @param bo 查询条件
     * @return 佣金清单字段列表
     */
    @Override
    public List<LzshCommissionHeaderVo> queryList(LzshCommissionHeaderBo bo) {
        LambdaQueryWrapper<LzshCommissionHeader> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LzshCommissionHeader> buildQueryWrapper(LzshCommissionHeaderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LzshCommissionHeader> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getCol00()), LzshCommissionHeader::getCol00, bo.getCol00());
        lqw.like(StringUtils.isNotBlank(bo.getCol01()), LzshCommissionHeader::getCol01, bo.getCol01());
        lqw.like(StringUtils.isNotBlank(bo.getCol02()), LzshCommissionHeader::getCol02, bo.getCol02());
        lqw.like(StringUtils.isNotBlank(bo.getCol03()), LzshCommissionHeader::getCol03, bo.getCol03());
        lqw.like(StringUtils.isNotBlank(bo.getCol04()), LzshCommissionHeader::getCol04, bo.getCol04());
        lqw.like(StringUtils.isNotBlank(bo.getCol05()), LzshCommissionHeader::getCol05, bo.getCol05());
        lqw.like(StringUtils.isNotBlank(bo.getCol06()), LzshCommissionHeader::getCol06, bo.getCol06());
        lqw.like(StringUtils.isNotBlank(bo.getCol07()), LzshCommissionHeader::getCol07, bo.getCol07());
        lqw.like(StringUtils.isNotBlank(bo.getCol08()), LzshCommissionHeader::getCol08, bo.getCol08());
        lqw.eq(StringUtils.isNotBlank(bo.getCol09()), LzshCommissionHeader::getCol09, bo.getCol09());
        lqw.eq(StringUtils.isNotBlank(bo.getCol10()), LzshCommissionHeader::getCol10, bo.getCol10());
        lqw.eq(StringUtils.isNotBlank(bo.getCol11()), LzshCommissionHeader::getCol11, bo.getCol11());
        lqw.eq(StringUtils.isNotBlank(bo.getCol12()), LzshCommissionHeader::getCol12, bo.getCol12());
        lqw.eq(StringUtils.isNotBlank(bo.getCol13()), LzshCommissionHeader::getCol13, bo.getCol13());
        lqw.eq(StringUtils.isNotBlank(bo.getCol14()), LzshCommissionHeader::getCol14, bo.getCol14());
        lqw.eq(StringUtils.isNotBlank(bo.getCol15()), LzshCommissionHeader::getCol15, bo.getCol15());
        lqw.eq(StringUtils.isNotBlank(bo.getCol16()), LzshCommissionHeader::getCol16, bo.getCol16());
        lqw.eq(StringUtils.isNotBlank(bo.getCol17()), LzshCommissionHeader::getCol17, bo.getCol17());
        lqw.eq(StringUtils.isNotBlank(bo.getCol18()), LzshCommissionHeader::getCol18, bo.getCol18());
        lqw.eq(StringUtils.isNotBlank(bo.getCol19()), LzshCommissionHeader::getCol19, bo.getCol19());
        lqw.eq(StringUtils.isNotBlank(bo.getCol20()), LzshCommissionHeader::getCol20, bo.getCol20());
        lqw.eq(StringUtils.isNotBlank(bo.getCol21()), LzshCommissionHeader::getCol21, bo.getCol21());
        lqw.eq(StringUtils.isNotBlank(bo.getCol22()), LzshCommissionHeader::getCol22, bo.getCol22());
        lqw.eq(StringUtils.isNotBlank(bo.getCol23()), LzshCommissionHeader::getCol23, bo.getCol23());
        lqw.eq(StringUtils.isNotBlank(bo.getCol24()), LzshCommissionHeader::getCol24, bo.getCol24());
        lqw.eq(StringUtils.isNotBlank(bo.getCol25()), LzshCommissionHeader::getCol25, bo.getCol25());
        lqw.eq(StringUtils.isNotBlank(bo.getCol26()), LzshCommissionHeader::getCol26, bo.getCol26());
        lqw.eq(StringUtils.isNotBlank(bo.getCol27()), LzshCommissionHeader::getCol27, bo.getCol27());
        lqw.eq(StringUtils.isNotBlank(bo.getCol28()), LzshCommissionHeader::getCol28, bo.getCol28());
        lqw.eq(StringUtils.isNotBlank(bo.getCol29()), LzshCommissionHeader::getCol29, bo.getCol29());
        lqw.eq(StringUtils.isNotBlank(bo.getCol30()), LzshCommissionHeader::getCol30, bo.getCol30());
        lqw.eq(StringUtils.isNotBlank(bo.getCol31()), LzshCommissionHeader::getCol31, bo.getCol31());
        lqw.eq(StringUtils.isNotBlank(bo.getCol32()), LzshCommissionHeader::getCol32, bo.getCol32());
        lqw.eq(StringUtils.isNotBlank(bo.getCol33()), LzshCommissionHeader::getCol33, bo.getCol33());
        lqw.eq(StringUtils.isNotBlank(bo.getCol34()), LzshCommissionHeader::getCol34, bo.getCol34());
        lqw.eq(StringUtils.isNotBlank(bo.getCol35()), LzshCommissionHeader::getCol35, bo.getCol35());
        lqw.eq(StringUtils.isNotBlank(bo.getCol36()), LzshCommissionHeader::getCol36, bo.getCol36());
        lqw.eq(StringUtils.isNotBlank(bo.getCol37()), LzshCommissionHeader::getCol37, bo.getCol37());
        lqw.eq(StringUtils.isNotBlank(bo.getCol38()), LzshCommissionHeader::getCol38, bo.getCol38());
        lqw.eq(StringUtils.isNotBlank(bo.getCol39()), LzshCommissionHeader::getCol39, bo.getCol39());
        lqw.eq(StringUtils.isNotBlank(bo.getCol40()), LzshCommissionHeader::getCol40, bo.getCol40());
        lqw.eq(StringUtils.isNotBlank(bo.getCol41()), LzshCommissionHeader::getCol41, bo.getCol41());
        lqw.eq(StringUtils.isNotBlank(bo.getCol42()), LzshCommissionHeader::getCol42, bo.getCol42());
        lqw.eq(StringUtils.isNotBlank(bo.getCol43()), LzshCommissionHeader::getCol43, bo.getCol43());
        lqw.eq(StringUtils.isNotBlank(bo.getCol44()), LzshCommissionHeader::getCol44, bo.getCol44());
        lqw.eq(StringUtils.isNotBlank(bo.getCol45()), LzshCommissionHeader::getCol45, bo.getCol45());
        lqw.eq(StringUtils.isNotBlank(bo.getCol46()), LzshCommissionHeader::getCol46, bo.getCol46());
        lqw.eq(StringUtils.isNotBlank(bo.getCol47()), LzshCommissionHeader::getCol47, bo.getCol47());
        lqw.eq(StringUtils.isNotBlank(bo.getCol48()), LzshCommissionHeader::getCol48, bo.getCol48());
        lqw.eq(StringUtils.isNotBlank(bo.getCol49()), LzshCommissionHeader::getCol49, bo.getCol49());
        return lqw;
    }

    /**
     * 新增佣金清单字段
     *
     * @param bo 佣金清单字段
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(LzshCommissionHeaderBo bo) {
        LzshCommissionHeader add = MapstructUtils.convert(bo, LzshCommissionHeader.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改佣金清单字段
     *
     * @param bo 佣金清单字段
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(LzshCommissionHeaderBo bo) {
        LzshCommissionHeader update = MapstructUtils.convert(bo, LzshCommissionHeader.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LzshCommissionHeader entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除佣金清单字段信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
